package com.iphone.theme;

import android.content.res.ColorStateList;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public abstract class TheCategoryItem {

    private int mId = 0;
    private int mColor;
    private boolean mCanDelete = true;
    private String mTitle;

    private final View mMainView;
    private final ImageView mIconDelete;
    private final ImageView mIconMain;
    private final ImageView mIconIndex;
    private final TextView mTextTitle;
    private final TextView mTextSummary;
    private final ImageView mIconCheck;

    private long mFolderId;

    public TheCategoryItem(LayoutInflater inflater) {
        this(inflater.inflate(R.layout.category_list_item, null));
    }

    public TheCategoryItem(View group) {
        mMainView = group;
        mIconDelete = (ImageView) group.findViewById(android.R.id.icon1);
        mIconMain = (ImageView) group.findViewById(android.R.id.icon2);
        mIconIndex = (ImageView) group.findViewById(android.R.id.icon);
        mTextTitle = (TextView) group.findViewById(android.R.id.title);
        mTextSummary = (TextView) group.findViewById(android.R.id.summary);
        mIconCheck = (ImageView) group.findViewById(android.R.id.checkbox);

        mIconDelete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                onDelete(TheCategoryItem.this);
            }
        });
        group.findViewById(android.R.id.edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mIconDelete.getVisibility() == View.GONE) {
                    onItemClick(TheCategoryItem.this);
                }
            }
        });
    }

    public TheCategoryItem setFolderId(long id) {
        mFolderId = id;
        mMainView.setTag(this);
        return this;
    }

    public long getFolderId() {
        return mFolderId;
    }

    public TheCategoryItem setTitle(String title) {
        mTitle = title;
        mTextTitle.setText(title);
        return this;
    }

    public TheCategoryItem setTitleSingleLine() {
        mTextTitle.setSingleLine();
        return this;
    }

    public TheCategoryItem setSummary(String summary) {
        mTextSummary.setText(summary);
        return this;
    }

    public TheCategoryItem setIndexColor(boolean show, int color) {
        mIconIndex.setImageTintList(ColorStateList.valueOf(color));
        mIconIndex.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
        return this;
    }

    public TheCategoryItem setIcon(int resId) {
        mIconMain.setImageResource(resId);
        mIconMain.setVisibility(View.VISIBLE);
        return this;
    }

    public TheCategoryItem setIconBackground(int resId) {
        mIconMain.setBackgroundResource(resId);
        mIconMain.setVisibility(View.VISIBLE);
        return this;
    }

    public TheCategoryItem setIconColorTint(int color) {
        mColor = color;
        mIconMain.setImageTintList(ColorStateList.valueOf(color));
        return this;
    }

    public TheCategoryItem setIconBackgroundColorTint(int color) {
        mColor = color;
        mIconMain.setBackgroundTintList(ColorStateList.valueOf(color));
        return this;
    }

    public TheCategoryItem setEditMode(boolean edit) {
        mIconDelete.setVisibility(edit ? mCanDelete ? View.VISIBLE : View.INVISIBLE : View.GONE);
        return this;
    }

    public TheCategoryItem setCanDelete(boolean enable) {
        mCanDelete = enable;
        return this;
    }

    public TheCategoryItem setSingleCheckMode(boolean checkMode) {
        mIconCheck.setVisibility(checkMode ? View.VISIBLE : View.GONE);
        mTextSummary.setVisibility(checkMode ? View.GONE : View.VISIBLE);
        mIconIndex.setVisibility(checkMode ? View.GONE : View.VISIBLE);
        return this;
    }

    public TheCategoryItem setChecked(boolean checked) {
        mIconCheck.setVisibility(checked ? View.VISIBLE : View.INVISIBLE);
        return this;
    }

    public int getId() {
        return mId;
    }

    public int getColor() {
        return mColor;
    }

    public TheCategoryItem setId(int id) {
        mId = id;
        return this;
    }

    public String getTitle() {
        return mTitle;
    }

    public View getView() {
        return mMainView;
    }

    public abstract void onItemClick(TheCategoryItem category);

    public abstract void onDelete(TheCategoryItem category);
}
